package com.qst.action.team;

import com.qst.BaseServlet;
import com.qst.ExamException;
import com.qst.RequestUtil;
import com.qst.WebUtil;
import com.qst.entity.Team;
import com.qst.service.TeamService;
import com.qst.service.impl.ServiceFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/team/save.action")
public class SaveServlet extends BaseServlet {
    private TeamService classTeamService = ServiceFactory
            .getService(TeamService.class);
    @Override
    protected void doAction(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 将表单数据组装为ClassTeam对象
        Team t = new Team();
        t.setName(RequestUtil.getString(req, "name"));
        t.setBeginYear(RequestUtil.getDate(req, "beginYear"));
        t.setStatus(RequestUtil.getInt(req, "status"));
        t.setCreatorId(getCurrentUser(req).getId());// 设置创建者
        try {
            classTeamService.saveTeam(t);
            WebUtil.redirect(req, resp, "/team/view.action?id=" + t.getId());
        } catch (ExamException ex) {
            addError(req, ex.getMessage());
            req.setAttribute("team", t);
            WebUtil.forward(req, resp, "/team/create.jsp");
        }
    }
}
